package com.syqc.monitor.ui.alarm

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.entity.AlarmCar
import com.syqc.net.ServiceHelper

/**
 *@author joker
 *@time   2023/12/28 16:32
 *@desc
 */
class AlarmCarVM : AutoDisposeViewModel() {
    private val _alarmsLive = MutableLiveData<ArrayList<AlarmCar>>()
    val alarmsLive: LiveData<ArrayList<AlarmCar>> = _alarmsLive
    private val allAlarm = ArrayList<AlarmCar>()


    fun getCarAlarms() {
        allAlarm.clear()
        ServiceHelper.getRepository().getCarAlarms().autoDispose(this)
            .subscribe({
                allAlarm.addAll(it)
                allAlarm.sort()
                search(upKey)
            }, error)
    }

    private var upKey: String = ""
    fun search(key: String) {
        upKey = key.uppercase()
        if (key.isEmpty()) {
            _alarmsLive.postValue(allAlarm)
        } else {
            _alarmsLive.postValue(allAlarm.filter {
                it.carPlate.uppercase().contains(upKey)
            } as ArrayList<AlarmCar>?)
        }
    }
}